<template>
	<!-- 抄电表 -->
	<view>
		<!-- 视图 -->
		<view class="page" v-if="!showSelect">
			<view class="sbbh">
				<view class="sbbh-l"> 设备编号 </view>
				<view class="sbbh-r">
					<view class="inp" @tap="showSelctFn()">请点击选择</view>
					<view class="scan" @click="scanCode()">
						<u-icon name="scan" color="#44D94B" size="36"></u-icon>
						<text>扫一扫</text>
					</view>
				</view>
			</view>

			<view v-if="meterInfor" class="xb-card" style="margin-top: 20rpx" @click="meterInfor ? '' : showSelctFn()">
				<view>
					<text>编码</text>
					<text>{{meterInfor.meterNum}}</text>
				</view>
				<view>
					<text>企业名称</text>
					<text>{{meterInfor.enterpriseName||'-'}}</text>
				</view>
				<view>
					<text>类型</text>
					<text>{{ meterInfor ? meterType : "-" }}</text>
				</view>
        <view v-if="meterType==='电表'">
          <text>倍率</text>
          <text>{{ meterInfor ? meterInfor.magnification : "-" }}</text>
        </view>
				<view>
					<text>用途</text>
					<text>{{
            meterInfor ? meterInfor.category : "-"
          }}</text>
				</view>

			</view>

			<view class="xb-info" v-if="meterInfor">
        <view>
          <text>是否超额</text>
          <radio-group @change="overMaxChange">
            <label>
              <radio value="1" :checked="meterInfor.overMax == '0'" />
              <span>否</span>
            </label>

            <label>
              <radio value="0" :checked="meterInfor.overMax == '1'" />
              <span>是</span>
            </label>
          </radio-group>
        </view>
				<view>
					<text>上月读数</text>
					<input v-model="addForm.lastValue" placeholder="请填写" type="number" placeholder-class="inputClass" />
				</view>
				<view>
					<text>本月读数</text>
					<input v-model="addForm.currentValue" placeholder="请填写" type="number"
						placeholder-class="inputClass" />
				</view>
			</view>

			<view class="xb-bottom-btn" hover-class="button-hover" v-if="meterInfor" @click="submitFn">
				提交
			</view>
		</view>

		<!-- 选择设备编号s -->
		<view class="bh-select" v-show="showSelect">
			<view class="bh-select-t">
				<view class="search-box">
					<view class="search">
						<u-icon name="search" color="#999999"></u-icon>
						<input type="text" :value="searchVal" placeholder="搜索" style="padding-left: 16rpx"
							@input="searchFn" />
					</view>
					<text @click="closeSelctFn()">取消</text>
				</view>
				<view class="select_t">
					<!-- 				<view class="select_col">
						位置
						<u-icon name="arrow-down" size="14" class="wicon"></u-icon>
					</view> -->
					<view class="select_col" :class="tabIndex == item.txt ? 'select_t_ac' : ''"
						@click="changeTab(item.val)" v-for="(item, index) in categorOption" :key="index">
						{{ item.txt }}
					</view>
				</view>
			</view>

			<mescroll-body :top="`${navBarHeight + 114}rpx`" class="select_scroll" ref="mescrollRef"
				@init="mescrollInit" :down="downOption" @down="downCallback" @up="upCallback">
				<view class="select_list_col" v-for="item in dataList" :key="item.id" @click="itemCheck(item)">
					<view class="select_list_col_l">
						<view class="select_list_col_l_l"></view>
						<view class="select_list_col_l_r">
							<view>
								{{ item.meterNum }}
							</view>
							<view class="b-l">
								<text v-if="item.parkNum">{{ item.parkNum}}区</text>
								<text v-if="item.enterpriseName">{{ item.enterpriseName }}</text>
							</view>
						</view>
					</view>
					<view class="select_list_col_r">
						{{ item.category}}
					</view>
				</view>
			</mescroll-body>
		</view>
		<!-- 选择设备编号e -->

		<postSuccess v-if="showSuccess">
			<view slot="txt" class="suc-btnbox">
				<view @click="navToDetails">查看提交信息</view>
				<view @click="chaoAway">继续抄表</view>
			</view>
		</postSuccess>
	</view>
</template>
<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";

	export default {
		mixins: [MescrollMixin], // 使用mixin
		name: "navbar",
		data() {
			return {
				searchVal: "",
				showSuccess: false,
				statusBarHeight: 20,
				showSel: true,
				/* 状态栏高度 */
				navBarHeight: 0,
				/* 导航栏高度 */
				windowWidth: 375,
				meterType: "",
				meterTypeNum: "",
				/* 窗口宽度 */
				/* 设定状态栏默认高度 */
				val: "",
				/* 导航栏搜索框的值 */

				checkNum: 0,
				showSelect: false,
				info: {},
				factory: "",
				tabIndex: "总表",
				downOption: {
					page: {
						num: 0,
						size: 10,
						time: null,
					},
					auto: false, //是否在初始化后,自动执行downCallback; 默认true
				},
				dataList: [],
				mescollb: null,
				nowValue: "", //当前读数
				placeList: [],
				meterInfor: null,
				searchTimout: null,
				categorOption: null,
				searchValue: "",
				addForm: {
					category: "",
					meterType: "",
					meterNum: "",
					meterId: "",
					lastValue: "",
					currentValue: "",
					feeCycle: "",
          overMax:'0',
          maxValue:10000
				},
			};
		},
		onLoad(option) {
			this.apis.commonApi.getDict("meter_category").then((res) => {
				this.categorOption = this.dictFilter(res.data);
			});
			this.categorOption = [1, 2, 3]
			this.addForm.feeCycle = this.$u.timeFormat(
				Date.parse(new Date()),
				"yyyy-mm-dd hh:MM:ss"
			);
			// 获取手机系统信息
			// 除了h5 app mp-alipay的情况下执行
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			this.navBarHeight = this.getStatusBarHeight();
			// #endif
			this.meterTypeNum = option.type;

			if (option.type == 0) {
				this.meterType = "水表";
			} else if (option.type == 1) {
				this.meterType = "电表";
			}

		},
		methods: {
			checkTap() {
				this.setData({
					checked: !this.data.checked,
				});
			},
			scanCode() {
				uni.scanCode({
					success: (res) => {
            this.apis.energyApi
                .meterScan({
                  meterNum: res.result
                })
                .then((res2) => {
                  if (res2.code == 200 ) {
                    let info = res2.data;
                    this.meterInfor = info;
                    this.addForm.lastValue = info.lastValue || "";
                    this.addForm.meterNum = info.meterNum;
                    this.addForm.meterType = info.meterType;
                    this.addForm.category = info.category;
                    this.addForm.meterId = info.id;
                    this.dataList = [];
                    this.showSelect = false;
                  } else {
                    uni.showToast({
                      icon: 'none',
                      title: '出现错误，请联系管理员',
                      duration: 3000
                    })
                  }

                });
					},
				});
			},
			changeTab(index) {
				this.dataList = [];
				this.tabIndex = index;
				this.mescroll.resetUpScroll();
			},
			searchFn(e) {
				this.dataList = [];
				this.searchVal = e.detail.value;
				clearTimeout(this.searchTimout);
				this.searchTimout = setTimeout(() => {
					this.mescroll.resetUpScroll();
				}, 1000);
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.dataList = []
				this.mescroll.resetUpScroll();
			},
			upCallback(page) {
				this.apis.energyApi
					.nyMeterAccount({
						pageNum: page.num,
						pageSize: page.size,
						meterType: this.meterType,
						category: this.tabIndex,
						meterNum: this.searchVal,
					})
					.then((res) => {
						this.dataList = this.dataList.concat(res.rows);
						this.mescroll.endBySize(res.rows.length, res.total);
					});

			},
      overMaxChange(op){
        this.meterInfor.overMax=this.addForm.overMax=op.detail.value
      },
			itemCheck(item) {
				console.log("item", item);
				this.meterInfor = item;
				this.addForm.lastValue = item.lastValue || "";
				this.addForm.meterNum = item.meterNum;
				this.addForm.meterType = item.meterType;
				this.addForm.category = item.category;
				this.addForm.meterId = item.id;
				this.dataList = [];
				this.showSelect = false;
			},
			showSelctFn() {
				this.dataList = [];
				this.showSelect = true;
				this.mescroll.resetUpScroll();
			},
			closeSelctFn() {
				this.dataList = [];
				this.showSelect = false;
			},
			submitFn() {
        this.addForm.overMax=this.meterInfor.overMax
				if (this.addForm.currentValue == "") {
					uni.showToast({
						icon: "none",
						title: "请输入当前读数",
					});
				} else if (parseInt(this.addForm.currentValue) < parseInt(this.addForm.lastValue)) {
					uni.showToast({
						icon: "none",
						title: "读数不能小于上期读数",
					});
				} else {
					this.addForm.currentCheckDate = this.$u.timeFormat(
						Date.parse(new Date()),
						"yyyy-mm-dd hh:MM:ss"
					);
					this.apis.energyApi.nyCheckMeterPut(this.addForm).then((res) => {
						if (res.code == 200) {
							this.showSuccess = true;
						}
					});
				}
			},
			//继续抄表
			chaoAway() {
				this.addForm = {
					category: "",
					meterType: "",
					meterNum: "",
					meterId: "",
					lastValue: "",
					currentValue: "",
          overMax:"0",
          maxValue:10000
				};
				this.meterInfor = null;
				this.showSuccess = false;
			},
			navToDetails() {
				this.navTo(
					"/pagesEnergy/hydroelectric/Details?meterId=" + this.addForm.meterId
				);
			}
		},
	};
</script>

<style lang="scss" scoped>
	// @import '../../common/css/icons.css';
	// @import './../../uni.less';

	.page {
		padding-bottom: 112rpx;
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #666666;
	}

	.sbbh {
		margin-bottom: 20rpx;
		box-sizing: border-box;
		padding: 30rpx 32rpx;
		width: 100%;
		background: #ffffff;
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.sbbh-l {
			font-size: 28rpx;
			font-family: PingFang-SC-Medium, PingFang-SC;
			font-weight: 500;
			color: #666666;
		}

		.sbbh-r {
			display: flex;

			.inp {
				margin-right: 8upx;
				font-size: 28upx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;

				&::after {
					content: "";
					position: absolute;
					top: 0;
					right: 0;
					height: 100%;
					width: 1rpx;
					background: #eeeeee;
				}
			}

			.scan {
				height: 100%;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #44d94b;

				u-icon {
					display: flex;
					align-items: center;
				}

				text {
					margin-left: 12rpx;
				}
			}
		}
	}

	.xb-card {
		padding: 0 32rpx;
		margin-bottom: 20rpx;

		view {
			padding-top: 28rpx;
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #eee;
			display: flex;
			justify-content: space-between;
			align-items: center;

			&:last-child {
				border: none;
			}
		}

		text:nth-child(2) {
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			color: #333333;
		}
	}

	.bh-select {
		.wicon {
			padding-top: 2rpx;
			height: 100%;
			margin-left: 12rpx;
			@include flexCenter;
		}

		.select_list_col {
			box-sizing: border-box;
			padding: 0 52rpx 0 24rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 100%;
			height: 148rpx;
			background-color: #fff;
			border-radius: 12rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border: none;
			}

			.b-l {
				text {
					margin-right: 20upx;
				}
			}

			.select_list_col_l {
				display: flex;
				align-items: center;

				.select_list_col_l_l {
					margin-right: 24rpx;
					width: 42rpx;
					height: 42rpx;
					border-radius: 50%;
					border: 1rpx solid #cecece;
				}

				.select_list_col_l_r {
					view:nth-child(1) {
						font-size: 28rpx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
					}

					view:nth-child(2) {
						margin-top: 6rpx;
						font-size: 24rpx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #666666;
						line-height: 34rpx;
					}
				}
			}

			.select_list_col_r {
				flex-shrink: 0;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
			}
		}

		.bh-select-t {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;

			.search-box {
				box-sizing: border-box;
				padding: 20rpx;
				top: 0;
				left: 0;
				width: 100vw;
				background-color: #fff;
				display: flex;
				align-items: center;

				view {
					border-radius: 12rpx;
					flex-grow: 1;
					height: 70rpx;
					display: flex;
					padding: 0 20rpx;
					align-items: center;
					background: #f5f5f5;
				}

				text {
					display: inline-flex;
					font-size: 24rpx;
					margin-left: 16rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
				}
			}

			.select_t {
				padding: 22rpx 40rpx;
				width: 100vw;
				background: #ffffff;
				display: flex;
				border-bottom: 1rpx solid #eeeeee;

				.select_col {
					box-sizing: border-box;
					min-width: 152rpx;
					margin-right: 20rpx;
					height: 46rpx;
					background: #f2f2f2;
					border-radius: 24rpx;
					font-size: 24rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
					@include flexCenter;
				}

				.select_t_ac {
					background: rgba(68, 217, 75, 0.1);
					border: 1rpx solid #44d94b;
					color: #44d94b;
				}
			}
		}
	}

	.suc-btnbox {
		margin-top: 60rpx;
		display: flex;

		view {
			@include flexCenter;
		}

		view:nth-child(1) {
			width: 220rpx;
			height: 80rpx;
			background: #f8f8f8;
			border-radius: 2rpx;
			font-size: 26rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #7b7b7b;
			border: 1rpx solid #dcdcdc;
		}

		view:nth-child(2) {
			margin-left: 40rpx;
			width: 220rpx;
			height: 80rpx;
			background: #f8f8f8;
			border-radius: 2rpx;
			font-size: 26rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #44d94b;
			border: 1rpx solid #44d94b;
		}
	}
</style>
